package com.ruoyi.instrumentation.controller;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.file.FileTypeUtils;
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import net.sf.jsqlparser.util.validation.metadata.DatabaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.instrumentation.domain.InstrumentInfo;
import com.ruoyi.instrumentation.service.IInstrumentInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 仪器设备信息Controller
 * 
 * @author hj
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/info")
public class InstrumentInfoController extends BaseController
{
    @Autowired
    private IInstrumentInfoService instrumentInfoService;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询仪器设备信息列表
     */
    @RequiresPermissions("instrumentation:info:list")
    @GetMapping("/list")
    public TableDataInfo list(InstrumentInfo instrumentInfo)
    {
        instrumentInfo.setDelFlag(0);
        startPage();
        List<InstrumentInfo> list = instrumentInfoService.selectInstrumentInfoList(instrumentInfo);
        return getDataTable(list);
    }

    /**
     * 导出仪器设备信息列表
     */
    @RequiresPermissions("instrumentation:info:export")
    @Log(title = "仪器设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InstrumentInfo instrumentInfo)
    {
        List<InstrumentInfo> list = instrumentInfoService.selectInstrumentInfoList(instrumentInfo);
        ExcelUtil<InstrumentInfo> util = new ExcelUtil<InstrumentInfo>(InstrumentInfo.class);
        util.exportExcel(response, list, "仪器设备信息数据");
    }

    /**
     * 获取仪器设备信息详细信息
     */
    @RequiresPermissions("instrumentation:info:query")
    @GetMapping(value = "/{instrumentId}")
    public AjaxResult getInfo(@PathVariable("instrumentId") Long instrumentId)
    {
        return success(instrumentInfoService.selectInstrumentInfoByInstrumentId(instrumentId));
    }

    /**
     * 新增仪器设备信息
     */
    @RequiresPermissions("instrumentation:info:add")
    @Log(title = "仪器设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InstrumentInfo instrumentInfo)
    {
        String username = SecurityUtils.getUsername();
        instrumentInfo.setCreateBy(username);
        return toAjax(instrumentInfoService.insertInstrumentInfo(instrumentInfo));
    }

    /**
     * 修改仪器设备信息
     */
    @RequiresPermissions("instrumentation:info:edit")
    @Log(title = "仪器设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InstrumentInfo instrumentInfo)
    {
        String username = SecurityUtils.getUsername();
        instrumentInfo.setUpdateBy(username);
        return toAjax(instrumentInfoService.updateInstrumentInfo(instrumentInfo));
    }

    /**
     * 删除仪器设备信息
     */
    @RequiresPermissions("instrumentation:info:remove")
    @Log(title = "仪器设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{instrumentIds}")
    public AjaxResult remove(@PathVariable Long[] instrumentIds)
    {
        //return toAjax(instrumentInfoService.deleteInstrumentInfoByInstrumentIds(instrumentIds));

        Integer delFlag = 1;
        String username = SecurityUtils.getUsername();
        Map<String, Object> params = new HashMap<>();
        params.put("delFlag", delFlag);
        params.put("deleteBy", username);
        params.put("deleteTime", new Date());

        if (instrumentIds.length == 1) {
            Long instrumentId = instrumentIds[0];
            params.put("instrumentId", instrumentId);
            return toAjax(instrumentInfoService.updateInstrumentInfoByinstrumentId(params));
        }
        params.put("instrumentIds", instrumentIds);
        return toAjax(instrumentInfoService.updateInstrumentInfoByinstrumentIds(params));
    }

    /**
     * 图片上传
     */
    @Log(title = "用户头像", businessType = BusinessType.INSERT)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file)
    {
        if (!file.isEmpty())
        {
            String extension = FileTypeUtils.getExtension(file);
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION))
            {
                return error("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
            }
            R<SysFile> fileResult = remoteFileService.upload(file);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
            {
                return error("文件服务异常，请联系管理员");
            }

            String url = fileResult.getData().getUrl();
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

    /**
     * 获取所有可用的仪器设备信息详细信息
     */
    @GetMapping("/getInstruments")
    public AjaxResult getInstruments()
    {
        List<InstrumentInfo> list = instrumentInfoService.getInstruments();
        return success(list);
    }
}
